package classes;

public class p4Thread extends Thread{
	private double left;
	private double right;
	private double fLeft;
	private double fRight;
	private double oldArea;
	private double mid; 
	private double fmid; 
	public double larea; 
	public double rarea; 
	private double epsilon;
	p4Thread t1 ;
	p4Thread t2; 
	
	
	public p4Thread(double left,double right,double fLeft, double fRight, double oldArea,double epsilon){
		this.left = left;
		this.right = right;
		this.fLeft= fLeft;
		this.fRight = fRight;
		this.oldArea = oldArea;
		this.epsilon = epsilon;
		Part4.setNoOfThreads();
		
	}
	
	public double getLarea(){
		return this.larea;
	}
	
	public double getRarea(){
		return this.rarea;
	}
	
	public void run() { 
		
		mid = (left + right)/2;
		fmid = Math.sqrt(1-mid*mid); 
		larea = (fLeft + fmid) * (mid - left)/2;
		rarea = (fmid + fRight) * (right-mid)/2;
		
		if (Math.abs((larea + rarea)-oldArea) >  epsilon ){
			 
			t1 = new p4Thread(left,mid,fLeft,fmid,larea,epsilon);
			t2 = new p4Thread(mid,right,fmid,fRight,rarea,epsilon);
			t1.start();
			t2.start();
			try {
				t1.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				t2.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			larea = t1.larea + t2.larea;
			rarea = t1.rarea + t2.rarea;

		
		}
		
			
		
 }

	
}
